﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.Models.BookShop;

namespace TL_Cloud.RepositoryService.BookShop
{
    public class BaseRepository
    {
        public readonly AppBookShopContext _myContext;
        public readonly IPropertyMappingContainer _propertyMappingContainer;
        public BaseRepository(AppBookShopContext myContext,
            IPropertyMappingContainer propertyMappingContainer)
        {
            this._myContext = myContext;
            this._propertyMappingContainer = propertyMappingContainer;
        }
    }

    public class BaseRepository<T>
        where T:BaseEntity 
    {
        public  DbSet<T> currentDbSet;
        public readonly AppBookShopContext _myContext;
        public readonly IPropertyMappingContainer _propertyMappingContainer;
        public BaseRepository(AppBookShopContext myContext,
            IPropertyMappingContainer propertyMappingContainer)
        {
            this._myContext = myContext;
            this._propertyMappingContainer = propertyMappingContainer;
        }
        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Update(T entity)
        {
            _myContext.Entry(entity).State = EntityState.Modified;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Delete(T entity)
        {
            currentDbSet.Remove(entity);
        }
        /// <summary>
        /// 增加新的数据
        /// </summary>
        /// <param name="entity"></param>
        public virtual void Add(T entity)
        {
            currentDbSet.Add(entity);
        }
        /// <summary>
        /// 根据主键获取对应的实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetEntityByIdAsync(string id)
        {
            return await currentDbSet.FindAsync(id);
        }

    }
}
